// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   RouteDao.java

package com.itheima.travel.dao;

import com.itheima.travel.model.Route;
import com.itheima.travel.model.RouteImg;
import com.itheima.travel.util.JdbcUtils;
import java.sql.SQLException;
import java.util.*;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class RouteDao
{

	private JdbcTemplate jdbcTemplate;

	public RouteDao()
	{
		jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
	}

	public List getPopularityRouteList()
		throws SQLException
	{
		String sql = "select * from tab_route where rflag='1' order by count desc limit 0,4";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(com/itheima/travel/model/Route));
	}

	public List getNewestRouteList()
		throws SQLException
	{
		String sql = "SELECT * FROM tab_route WHERE rflag='1' ORDER BY rdate DESC LIMIT 0,4";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(com/itheima/travel/model/Route));
	}

	public List getThemeRouteList()
		throws SQLException
	{
		String sql = "SELECT * FROM tab_route WHERE rflag='1' and isThemeTour='1' ORDER BY rdate DESC LIMIT 0,4";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(com/itheima/travel/model/Route));
	}

	public List findRouteListByPage(int cid, int curPage, int pageSize, String rname)
		throws SQLException
	{
		String sql = "SELECT * FROM tab_route  WHERE  rflag='1' and cid=? ";
		List paramsList = new ArrayList();
		paramsList.add(Integer.valueOf(cid));
		if (rname != null && !"".equals(rname))
		{
			sql = (new StringBuilder()).append(sql).append(" and rname like ? ").toString();
			paramsList.add((new StringBuilder()).append("%").append(rname).append("%").toString());
		}
		sql = (new StringBuilder()).append(sql).append(" LIMIT ?,?").toString();
		int start = (curPage - 1) * pageSize;
		paramsList.add(Integer.valueOf(start));
		paramsList.add(Integer.valueOf(pageSize));
		Object params[] = paramsList.toArray();
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(com/itheima/travel/model/Route), params);
	}

	public int getCountByCid(int cid, String rname)
		throws SQLException
	{
		String sql = "SELECT COUNT(*) FROM tab_route WHERE rflag='1' AND cid=? ";
		List paramsList = new ArrayList();
		paramsList.add(Integer.valueOf(cid));
		if (rname != null && !"".equals(rname))
		{
			sql = (new StringBuilder()).append(sql).append(" and rname like ? ").toString();
			paramsList.add((new StringBuilder()).append("%").append(rname).append("%").toString());
		}
		return ((Integer)jdbcTemplate.queryForObject(sql, paramsList.toArray(), java/lang/Integer)).intValue();
	}

	public Map findRouteByRid(String rid)
		throws SQLException
	{
		String sql = "SELECT * FROM tab_route r,tab_category c,tab_seller s WHERE r.cid=c.cid AND r.sid=s.sid AND r.rflag='1' AND r.rid=?";
		return jdbcTemplate.queryForMap(sql, new Object[] {
			rid
		});
		DataAccessException e;
		e;
		e.printStackTrace();
		return null;
	}

	public List findRouteImgsByRid(String rid)
		throws SQLException
	{
		String sql = "SELECT * FROM tab_route_img WHERE rid=?";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(com/itheima/travel/model/RouteImg), new Object[] {
			rid
		});
	}
}
